package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootWz;
import org.footballmanager.pandafootball.system.form.impl.FootWzSearchForm;
import org.footballmanager.pandafootball.system.service.FootWzService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class FootWzServiceImpl extends ServiceImpl<FootWz> implements FootWzService {
    /**
     * 根据所传球员位置，查找位置信息
     *
     * @param wz 位置
     * @return 位置字符串
     */
    @Override
    public String findPositionStringByWz(String wz) {
        FootWz footWz = this.findPositioniByPersonPosition(wz);
        return footWz.getWzmc();
    }

    /**
     * 根据所传球员位置，查找位置信息
     *
     * @param wz 位置
     * @return 实体类
     */
    @Override
    public FootWz findPositioniByPersonPosition(String wz) {
        Example example = new Example(FootWz.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("yw", wz.trim());
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 根据位置编码获取主键
     *
     * @param wz 位置编码
     * @return 主键
     */
    @Override
    public Long findByWzbm(String wz) {
        Example example = new Example(FootWz.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wzbm", wz.trim());
        criteria.andEqualTo("delFlag", 0);
        FootWz footWz = findByExample(example);
        if (footWz != null) {
            return footWz.getId();
        }
        return null;
    }

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footWzSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootWzSearchForm footWzSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footWzSearchForm);
        Page<FootWz> page = PageHelper.startPage(footWzSearchForm.getPageIndex(), footWzSearchForm.getPageSize());
        List<FootWz> list = this.findall(footWzSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footWzSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootWzSearchForm footWzSearchForm, HttpServletRequest request) {
        if (footWzSearchForm.getSelectIds() != null) {
            String[] result = footWzSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }
}
